<template>
	<view class="content">
		<view class="rg_ico">
			<image src="../static/imgs/logo01.jpg" />
		</view>
		<p class="top_p">欢迎您的注册</p>
		<form @submit="rg_submit">
			<input type="text" name="phone" placeholder="请输入手机号">
			<!-- <input type="text" placeholder="请输入验证码"> -->
			<input type="password" name="password" placeholder="请输入密码">
			<input type="password" name="againPwd" placeholder="请再次确认密码">
			<input type="text" name="invitation" placeholder="请输入邀请码">
			<!-- 邮箱验证码 -->
			<input name="email" type="text" @blur="getemail" placeholder="请输入邮箱号" id="form-code">
			<input name="Codes" type="text" placeholder="验证码" id="form-code" class=" lastInput">
			<button class="but1" @click="refreshCode" :disabled="isDisable">点击发送邮箱验证码</button>
			<!-- 图片/验证码 -->
			<!-- <br> -->
			<!-- <div class="remove-padding border center-block yzm" >
					<span class="code-img"><img  :src="imgurl"/></span>
					<br>
					<button id = "but" @click="refreshCode":disabled="isDisable">看不清？换一张</button>
				</div> -->

			<button form-type="submit" class="but">注册</button>
		</form>
	</view>
</template>

<script>
	import {
		REGISTER,
		BASE_URL,
		EMAIL
	} from "../constants/index.js"

	export default {
		// props: {
		//   width: {
		//     type: String,
		//     default: '100px'
		//   },
		//   height: {
		//     type: String,
		//     default: '34px'
		//   },
		//   length: {
		//     type: Number,
		//     default: 4
		//   }
		// },
		// data () {
		//   return {
		//     codeList: []
		//   }
		// },
		// mounted () {
		//   this.createdCode()
		// },
		data() {
			return {
				imgurl: BASE_URL + '/app/member/imgs',
				isDisable: false,
				email: '',
			}
		},
		methods: {
			getemail(e) {
				this.email = e.detail.value;
			},

			// --------------点击注册
			rg_submit(e) {
				e.preventDefault();
				// 注册（填写的信息）
				let data = e.detail.value;
				// 校验
				// if(data.phone.length!=11){
				// 	return uni.showToast({
				// 		title: '请输入正确格式的手机号码！',
				// 	});
				// }


				var re = /^1[1356789]\d{9}$/;
				let str = data.phone;
				if (!re.test(str)) {
					return uni.showToast({
						title: '请确认电话格式！',
					});
				}


				if (data.password.length < 6) {
					return uni.showToast({
						title: '密码长度不能少于6位！',
					});
				}
				if (data.password !== data.againPwd) {
					return uni.showToast({
						title: '两次密码不一致！',
					});
				}
				if (data.email == true) {

					return uni.showToast({
						title: '请输入邮箱！',
					});
				}

				if (data.Codes == null) {
					return uni.showToast({
						title: '请输入验证码！',
					});
				}


				// --------------校验完毕

				// --------------后台数据交互
				this.$request.post(REGISTER, data).then(res => {
					//手机号码已注册 注册失败
					if (res == 3) {
						return uni.showToast({
							title: "用户已被注册！"
						});
					}
					//邮箱 注册失败
					if (res == 4) {
						console.log(data.invitation)
						return uni.showToast({
							title: "邮箱已被绑定，请重新输入！"
						});
					}
					//邀请码 注册失败
					if (res == 2) {
						console.log(data.invitation)
						return uni.showToast({
							title: "邀请码无效请重新输入！"
						});
					}
					//验证码 注册失败
					if (res == 0) {
						return uni.showToast({
							title: "验证码错误，注册失败！",

						});
					} else if (res == 1) {
						// 注册成功
						uni.showToast({
							title: "注册成功！",
						});
					}
					
					// 跳转页面
					setTimeout(function(){
						uni.reLaunch({
							url: './login'
						})
					},2000)


				}).catch((e) => {});


			},
			// 刷新验证码

			// 获取开关
			refreshCode: function() {
				this.isDisable = true
				setTimeout(() => {
					this.isDisable = false //点击一次时隔60秒后才能再次点击
				}, 3000)
				var format = /^^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
				if (this.email == null) {
					return uni.showToast({
						title: '请输入邮箱！',
					});
				}
				if (!format.test(this.email)) {
					return uni.showToast({
						title: '邮箱格式不正确！',
					});
				}
				//邮箱获取
				this.$request.post(EMAIL, {email: this.email}).then(res => {
					console.log(123)
					if (res == true) {
						return uni.showToast({
							title: "邮箱验证码发送成功，请注意查看邮件",
						});
					}
					return uni.showToast({
						title: "邮箱验证码发送失败，请注意邮箱格式",
					});
				})


				// uni.request({
				// 	url: "http://192.168.0.209:8888" + EMAIL,
				// 	method:'POST',
				// 	data:{email:'111111'}

				// }).then((response)=>{
				// 	console.log(response);
				// 	// let [error, res] = response;
				// 	// resolve(res.data);
				// 	// //console.log(response[1].header.token); //获取请求头token
				// });




				//图形化验证码刷新
				// var date = new Date();
				// this.imgurl=BASE_URL+'/app/member/imgs?date='+date.getTime()
				// uni.request({
				// 	url: this.imgurls,
				// 	success: (res) => {

				// 	},
				// });
			},
		}
	}


	// 验证码
</script>

<style>
	/* .ValidCode{
		display: flex;
		justify-content: center;
		align-items: center;
		cursor: pointer;
		span{
		  display: inline-block;
		}
	  } */
	/* 页面内部大小控制 */
	/* .yzm{
		display: inline;
		border: 1px solid #2C405A;
		position: relative;
		right: 20rpx;
	} */
	span {
		margin-left: 32rpx;
	}

	.content {
		padding: 0 50rpx;
	}

	.rg_ico {
		padding-top: 25rpx;
		text-align: center;
	}

	.rg_ico image {
		width: 130rpx;
		height: 130rpx;
		border-radius: 80rpx;
	}

	.code-img {
		display: inline;
		border: 1px #007AFF solid;
	}

	.top_p {
		padding-top: 20rpx;
		text-align: center;
	}

	input {
		margin-top: 40rpx;
		padding-left: 20rpx;
		font-size: 13px;
		border-radius: 20rpx;
		height: 80rpx;
		border: 1px #C0C0C0 solid;
	}

	/* input:last-child{
		width:20rpx;
		border: 1px #09BB07 ;
	} */
	.lastInput {
		width: 120px;
		display: inline-block;
		float: left;
	}

	.but1 {
		display: inline-block;
		width: 150px;
		font-size: 14px;
		padding: 0;
		margin-left: 0px;
		border-radius: 5px;
		text-align: center;
		float: right;
	}

.but {
		display: inline-block;
		width: 100%;
		font-size: 14px;
		padding: 0;
		margin-left: 0px;
		border-radius: 15px;
		text-align: center;
	}
	button {
		margin-top: 40rpx;
		border-radius: 40rpx;
		background: rgb(247, 149, 184);
		color: white;
		height: 88rpx;
		font-size: 15px;
	}
</style>
